# Maintainer: LH_Mouse <lh_mouse@126.com>

_realname=mcfgthread
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}"-libs)
pkgver=r521.9570e5c
pkgrel=1
pkgdesc="An efficient Gthread implementation for GCC MinGW-w64 targets on Windows, providing both C11 and C++11 thread support"
arch=('any')
url="https://github.com/lhmouse/mcfgthread"
license=('LGPL' 'custom')
options=('staticlibs' 'strip')
_branch=master
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-crt"
             "${MINGW_PACKAGE_PREFIX}-headers")
source=("git+https://github.com/lhmouse/mcfgthread.git")
sha256sums=('SKIP')

pkgver() {
  cd "${_realname}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"

  mkdir -p m4
  autoreconf -ifv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make

  rm -rf "${srcdir}${MINGW_PREFIX}"
  make -j1 DESTDIR=${srcdir} install
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package_mingw-w64-mcfgthread() {
  pkgdesc="An efficient Gthread implementation for GCC MinGW-w64 targets on Windows, providing both C11 and C++11 thread support (devel)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"-libs)

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  _files_cp="$(find bin -type f -and -not -name "*.dll")"
  [[ -z "${_files_cp}" ]] || cp --parents -r ${_files_cp} --target-directory="${pkgdir}${MINGW_PREFIX}"
  cp --parents -r include   --target-directory="${pkgdir}${MINGW_PREFIX}"
  cp --parents -r lib       --target-directory="${pkgdir}${MINGW_PREFIX}"
}
package_mingw-w64-mcfgthread-libs() {
  pkgdesc="An efficient Gthread implementation for GCC MinGW-w64 targets on Windows, providing both C11 and C++11 thread support (libs)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}"
  cd "${srcdir}${MINGW_PREFIX}"
  _files_cp="$(find bin -type f -and -name "*.dll")"
  [[ -z "${_files_cp}" ]] || cp --parents -r ${_files_cp} --target-directory="${pkgdir}${MINGW_PREFIX}"
}

# Wrappers for package functions

# 32-bit wrappers
package_mingw-w64-i686-mcfgthread() {
  package_mingw-w64-mcfgthread
}
package_mingw-w64-i686-mcfgthread-libs() {
  package_mingw-w64-mcfgthread-libs
}

# 64-bit wrappers
package_mingw-w64-x86_64-mcfgthread() {
  package_mingw-w64-mcfgthread
}
package_mingw-w64-x86_64-mcfgthread-libs() {
  package_mingw-w64-mcfgthread-libs
}
